using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data.SqlClient;

namespace Acms.SqlDal
{
    public class TopicDal : Base   
    {
        public TopicDal() : base() { }

        public enum Fields
        {
            Id,
            SiteId,
            Name,
            Sequence
        }

        protected override void ReadRecord(Hashtable table, SqlDataReader reader)
        {
            GetReaderValue<int>(Fields.Id, reader, table);
            GetReaderValue<int>(Fields.SiteId, reader, table);
            GetReaderValue<string>(Fields.Name, reader, table);
            GetReaderValue<int>(Fields.Sequence, reader, table);
        }

        public Hashtable[] GetAllBySiteId(int siteId)
        {
            SqlCommand cmd = CreateCommand("GetAllBySiteId");
            AddInputParameter(cmd, "SiteId", siteId, System.Data.SqlDbType.Int);
            return ExecuteGetCollection(cmd);
        }

        public Hashtable[] GetAllByPostId(int postId)
        {
            SqlCommand cmd = CreateCommand("GetAllByPostId");
            AddInputParameter(cmd, "PostId", postId, System.Data.SqlDbType.Int);
            return ExecuteGetCollection(cmd);
        }


        public Hashtable GetById(int id)
        {
            SqlCommand cmd = CreateCommand("GetById");
            AddInputParameter(cmd, "Id", id, System.Data.SqlDbType.Int);
            return ExecuteGetItem(cmd);
        }

        public void Update(ref int id, int siteId, string name, ref int sequence, ref object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Put");
            AddInputParameter(cmd, "Id", id, System.Data.SqlDbType.Int);
            AddInputParameter(cmd, "SiteId", siteId, System.Data.SqlDbType.Int);
            AddInputParameter(cmd, "Name", name, System.Data.SqlDbType.VarChar, 100);
            AddInputParameter(cmd, "Sequence", sequence, System.Data.SqlDbType.Int);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, System.Data.SqlDbType.Timestamp);
            AddOutputParameter(cmd, "IdOut", System.Data.SqlDbType.Int);
            AddOutputParameter(cmd, "SequenceOut", System.Data.SqlDbType.Int);
            AddOutputParameter(cmd, "ConcurencyIdOut", System.Data.SqlDbType.Timestamp);

            ExecuteCommand(cmd);

            id = GetOutputValue<int>(cmd, "IdOut");
            sequence = GetOutputValue<int>(cmd, "SequenceOut");
            concurencyId = GetOutputValue<byte[]>(cmd, "ConcurencyIdOut");
        }

        public void Delete(int id, object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Delete");
            AddInputParameter(cmd, "Id", id, System.Data.SqlDbType.Int);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, System.Data.SqlDbType.Timestamp);

            ExecuteCommand(cmd);
        }

    }
}
